package com.iot.inclinometer.ui;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.iot.inclinometer.R;
import com.iot.inclinometer.util.MyAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * "选择文件夹"
 *
 * @author WXR
 */
public class MyFileManager extends ListActivity {
    private List<String> items = null;
    private List<String> paths = null;
    private String rootPath = getSDPath(MyFileManager.this);
    private String curPath = "/";
    private TextView mPath;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.fileselect);
        mPath = (TextView) findViewById(R.id.mPath);
        Button buttonConfirm = (Button) findViewById(R.id.buttonConfirm);
        buttonConfirm.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent data = new Intent(MyFileManager.this, OutputProUI.class);
                Bundle bundle = new Bundle();
                bundle.putString("file", curPath);
                data.putExtras(bundle);
                setResult(2, data);
                finish();

            }
        });
        Button buttonCancle = (Button) findViewById(R.id.buttonCancle);
        buttonCancle.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                finish();
            }
        });
        getFileDir(rootPath);
    }

    /*
     * 设置文件路径
     * */
    private void getFileDir(String filePath) {
        mPath.setText(filePath);
        items = new ArrayList<String>();
        paths = new ArrayList<String>();
        File f = new File(filePath);
        File[] files = f.listFiles();

        if (null != files) {

            if (!filePath.equals(rootPath)) {
                items.add("b1");
                paths.add(rootPath);
                items.add("b2");
                paths.add(f.getParent());
            }
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                items.add(file.getName());
                paths.add(file.getPath());
            }

        }

        setListAdapter(new MyAdapter(this, items, paths));
    }

    private String getSDPath(Context context) {
        String sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory().getAbsolutePath();// 获取根目录
        } else {// 提示没检测到SD卡
            Toast.makeText(context, "没检测到SD卡！", Toast.LENGTH_SHORT).show();
            sdDir = "/";
        }
        return sdDir;
    }

    /*
     * 打开文件列表
     * */
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        File file = new File(paths.get(position));
        if (file.isDirectory()) {
            curPath = paths.get(position);
            getFileDir(paths.get(position));
        } else {
            openFile(file);
        }
    }

    /*
     * 打开文件
     * */
    private void openFile(File f) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);

        String type = getMIMEType(f);
        intent.setDataAndType(Uri.fromFile(f), type);
        startActivity(intent);
    }

    /*
     * 获取文件类型
     * */
    private String getMIMEType(File f) {
        String type = "";
        String fName = f.getName();
        String end = fName
                .substring(fName.lastIndexOf(".") + 1, fName.length()).toLowerCase();

        if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")
                || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
            type = "audio";
        } else if (end.equals("3gp") || end.equals("mp4")) {
            type = "video";
        } else if (end.equals("jpg") || end.equals("gif") || end.equals("png")
                || end.equals("jpeg") || end.equals("bmp")) {
            type = "image";
        } else {
            type = "*";
        }
        type += "/*";
        return type;
    }
}